package com.wxzz.elearing.user.api.admin;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.user.dto.BindWxUserDto;
import com.wxzz.elearing.user.dto.CurWxUserDto;
import com.wxzz.elearing.user.dto.EnterpriseWxUserVisitNumStatisticsDto;
import com.wxzz.elearing.user.dto.WxUserDto;
import com.wxzz.elearing.user.entity.AdminUser;
import com.wxzz.elearing.user.entity.WxUser;
import com.wxzz.elearing.user.service.IWxUserService;
import com.wxzz.elearing.user.vo.EnterpriseAllStatisticsVo;
import com.wxzz.elearing.user.vo.EnterpriseStatisticsVo;
import com.wxzz.elearing.user.vo.EnterpriseWxUserVisitNumStatisticsVo;
import com.wxzz.elearing.user.vo.WxUserVisitNumStatisticsVo;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/user")
@RefreshScope
@Api(tags = "微信用户接口(后台)")
public class UserController {

    @Autowired
    private IWxUserService wxUserService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "导出企业用户信息(后台)", notes = "导出企业用户信息(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "exportExcel", method = RequestMethod.POST)
    public void exportExcel(HttpServletResponse response) throws IOException {
        wxUserService.exportExcel(response);
    }

    @ApiOperation(value = "导入企业用户信息(后台)", notes = "导入企业用户信息(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "importExcel", method = RequestMethod.POST)
    public void importExcel(@RequestHeader String token, @RequestParam("file") MultipartFile file) throws IOException {
        wxUserService.importExcel(token, file);
    }

    @ApiOperation(value = "平台用户列表(总后台)", notes = "平台用户列表(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listPlatformWxUser", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<WxUser>>> listPlatformWxUser(HttpServletResponse response,@RequestBody WxUserDto wxUserDto) {
        return wxUserService.listWxUser(response,0, wxUserDto, null);
    }

    @ApiOperation(value = "企业用户列表(总后台)", notes = "企业用户列表(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listEnterpriseWxUser", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<WxUser>>> listEnterpriseWxUser(HttpServletResponse response,@RequestBody WxUserDto wxUserDto) {
        return wxUserService.listWxUser(response,1, wxUserDto, null);
    }

    @ApiOperation(value = "添加职员用户(总后台)", notes = "添加职员用户(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "saveWxUser", method = RequestMethod.POST)
    public ReturnVO saveWxUser(@RequestHeader String token, @RequestBody WxUser wxUser) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        wxUser.setEnterpriseId(adminUser.getEnterpriseId());
        return wxUserService.saveWxUser(wxUser);
    }


    @ApiOperation(value = "列出绑定人员(后台)", notes = "列出绑定人员(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listBindWxUser", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<WxUser>>> listBindWxUser(@RequestHeader String token, @Valid @RequestBody BindWxUserDto bindWxUserDto) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        return wxUserService.listBindWxUser(adminUser.getEnterpriseId(), bindWxUserDto);
    }

    @ApiOperation(value = "根据当前用户身份调用微信用户列表(后台)", notes = "根据当前用户身份调用微信用户列表(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listCurWxUser", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<WxUser>>> listCurWxUser(HttpServletResponse response,@RequestHeader String token, @RequestBody WxUserDto wxUserDto) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        return wxUserService.listWxUser(response,null, wxUserDto, adminUser.getEnterpriseId());
    }

    @ApiOperation(value = "token换取用户信息(总后台)", notes = "token换取用户信息(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "selectAdminUserByToken", method = RequestMethod.POST)
    public ReturnVO<AdminUser> selectAdminUserByToken(@RequestHeader String token) {
        String s = redisTemplate.opsForValue().get(token);
        AdminUser adminUser = JSONObject.parseObject(s, AdminUser.class);
        return new ReturnVO<>("用户信息换取成功", adminUser);
    }

    @ApiOperation(value = "根据id查询后台用户(后台)", notes = "根据id查询后台用户(后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "selectAdminUserById/{adminUserId}", method = RequestMethod.POST)
    public ReturnVO<AdminUser> selectAdminUserById(@PathVariable Long adminUserId) {
        return wxUserService.selectAdminUserById(adminUserId);
    }

    @ApiOperation(value = "根据id查询微信用户(后台)", notes = "根据id查询微信用户(后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "selectWxUserId/{wxUserId}", method = RequestMethod.POST)
    public ReturnVO<WxUser> selectWxUserId(@PathVariable Long wxUserId) {
        return wxUserService.selectWxUserId(wxUserId);
    }

    @ApiOperation(value = "近30天用户数据(概况总览)(后台)", notes = "近30天用户数据(概况总览)(后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "selectWxUserVisitNumStatisticsVo", method = RequestMethod.POST)
    public ReturnVO<WxUserVisitNumStatisticsVo> selectWxUserVisitNumStatisticsVo() {
        return wxUserService.selectWxUserVisitNumStatisticsVo();
    }

    @ApiOperation(value = "近30天企业数据(概况总览)(后台)", notes = "近30天企业数据(概况总览)(后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "selectEnterpriseStatisticsVo", method = RequestMethod.POST)
    public ReturnVO<EnterpriseStatisticsVo> selectEnterpriseStatisticsVo() {
        return wxUserService.selectEnterpriseStatisticsVo();
    }

    @ApiOperation(value = "查询企业登录人数(概况总览)(后台)", notes = "近30天企业数据(概况总览)(后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "selectEnterpriseWxUserVisitNumStatisticsVo", method = RequestMethod.POST)
    public ReturnVO<EnterpriseWxUserVisitNumStatisticsVo> selectEnterpriseWxUserVisitNumStatisticsVo(@RequestHeader String token, @RequestBody EnterpriseWxUserVisitNumStatisticsDto enterpriseWxUserVisitNumStatisticsDto) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        return wxUserService.selectEnterpriseWxUserVisitNumStatisticsVo(adminUser.getEnterpriseId(), enterpriseWxUserVisitNumStatisticsDto);
    }

    @ApiOperation(value = "查询该企业的综合数据(概况总览)(后台)", notes = "查询该企业的综合数据(概况总览)(后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "selectEnterpriseAllStatisticsVo", method = RequestMethod.POST)
    public ReturnVO<EnterpriseAllStatisticsVo> selectEnterpriseAllStatisticsVo(@RequestHeader String token) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        return wxUserService.selectEnterpriseAllStatisticsVo(adminUser.getEnterpriseId());
    }

    @ApiOperation(value = "删除平台用户管理 企业用户管理(后台)", notes = "删除平台用户管理 企业用户管理(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteByWxUserId/{wxUserId}", method = RequestMethod.POST)
    public ReturnVO deleteByWxUserId(@PathVariable Long wxUserId) {
        return wxUserService.deleteByWxUserId(wxUserId);
    }

    @ApiOperation(value = "将用户移出企业(后台)", notes = "将用户移出企业(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "removeEnterprise/{wxUserId}", method = RequestMethod.POST)
    public ReturnVO removeEnterprise(@PathVariable Long wxUserId) {
        return wxUserService.removeEnterprise(wxUserId);
    }

}

